<?php
include 'inner/init.php'; 
protect_page();
include 'includes/overall/header.php';

if (empty($_POST) === false){
    $required_fields = array('fname', 'lname', 'email');
    foreach($_POST as $key=>$value){
        if (empty($value) && in_array($key, $required_fields) === true){
            $error[] = 'Please fill all fields.';
            break 1;
        }
    }
    if (empty($error) === true){
            if (email_exists($_POST['email']) === true && $userdata['email'] !== $_POST['emailaddress']){
            $error[] = 'Sorry, the email \'' . $_POST['email'] . '\' is already in use'; 
        }
    }
}    
?>
<h1>Settings</h1>

<?php
if (isset($_GET['success']) === true && empty($_GET['success']) === true){
    echo 'Your details have been updated!';
} else{
if (empty($_POST) === false && empty($error) === true){
    $update_data = array (
        'fname' => $_POST['fname'],
        'lname' => $_POST['lname'],
        'email' => $_POST['email'],
    );
    
    update_prof($update_data);
    header('Location: editprof.php?success');
    exit();
    
} else if (empty($error) === false){
    echo output_errors($error);
}
?>
<div class="widget"> 
	<div class="inner2">  
<form action="" method="post">
    <ul>
        <li>
            First name:<br>
            <input type="text" name="firstname" value="<?php echo $userdata['firstname'];?>">
        </li>
        <li>
            Last name:<br>
            <input type="text" name="lastname" value="<?php echo $userdata['lastname'];?>">
        </li>
        <li>
            Email:<br>
            <input type="text" name="emailaddress" value="<?php echo $userdata['emailaddress'];?>">
        </li>
        <li>
            <input type="submit" value="Update Info">
        </li>
    </ul>
            </div>
</div>       
</form>

<?php
}
include 'includes/overall/footer.php';
?>
